package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.brand.TbBrand;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.type_template.TbTypeTemplate;
import com.dongdongshop.service.brand.BrandService;
import com.dongdongshop.service.type_template.TypeTemPlateService;
import com.dongdongshop.service.specification.SpecificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    @Reference
    private TypeTemPlateService typeTemPlateService;
    @Reference
    private BrandService brandService;
    @Reference
    private SpecificationService specificationService;

    private final Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);
    //跳转模板页面
    @RequestMapping("typeTemplateShow")
    private String toTypeTemplateShow(Model model){
        List<TbBrand> listBrand = brandService.listBrand();
        List<TbSpecification> listSpecification = specificationService.listSpecificationEcho();
        model.addAttribute("listBrand",listBrand);
        model.addAttribute("listSpecification",listSpecification);
        return "admin/type_template";
    }
    //查询模板数据
    @RequestMapping("listTypeTemplate")
    @ResponseBody
    private Result listTypeTemplate(){
        try {
            List<TbTypeTemplate> list = typeTemPlateService.listTypeTemplate();
            return Result.ok().setData(list);
        }catch (Exception e){
            logger.error("查询模板信息错误",e);
            return Result.fail();
        }

    }
    //添加模板数据
    @RequestMapping("insertTypeTemplate")
    @ResponseBody
    public Result insertTypeTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            int i = typeTemPlateService.insertTypeTemplate(tbTypeTemplate);
            return Result.ok();
        }catch (Exception e){
            logger.error("添加模板错误",e);
            return Result.fail();
        }
    }
    //修改回显模板数据
    @RequestMapping("getTypeTemplate")
    @ResponseBody
    public Result getTypeTemplate(Long id){
        try {
            TbTypeTemplate tbTypeTemplate = typeTemPlateService.getTypeTemplate(id);
            return Result.ok().setInfo(tbTypeTemplate);
        }catch (Exception e){
            logger.error("修改回显模板错误",e);
            return Result.fail();
        }
    }
    //修改模板数据
    @RequestMapping("updateTypeTemplate")
    @ResponseBody
    public Result updateTypeTemplate(TbTypeTemplate tbTypeTemplate){
        try {
            int i = typeTemPlateService.updateTypeTemplate(tbTypeTemplate);
            return Result.ok();
        }catch (Exception e){
            logger.error("修改模板错误",e);
            return Result.fail();
        }
    }

    //批量删除模板数据
    @RequestMapping("deleteTypeTemplateBatch")
    @ResponseBody
    public Result deleteTypeTemplateBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = typeTemPlateService.deleteTypeTemplateBatch(ids);
            return Result.ok();
        }catch (Exception e){
            logger.error("删除模板错误",e);
            return Result.fail();
        }
    }

}
